﻿
#include "MainWindow.hpp"




NAMESPACE_BEGIN(NSMainWindow)



MainWindow* MainWindow::ins = nullptr;

void MainWindow::Initialize() {
    /*全局变量-必须最先调用*/
    globalVariable();
    /*初始化界面*/
    surfaceEngineSetup();
    showMaximized();
    
}

void MainWindow::Finalize() {
    if (logo_btn != nullptr) delete logo_btn;
    if (status_bar != nullptr) delete status_bar;
    if (tab_dock != nullptr) delete tab_dock;


}



void MainWindow::globalVariable(){
    mouse_pos = new QPushButton(this);
    //signal_handle = new NSPMainSignals::MainSignals();
    status_bar = new QStatusBar(this);
    tab_dock = new QDockWidget(this);
    logo_btn = new QToolButton(this);
    current_dock = new QDockWidget(this);
}



void MainWindow::surfaceEngineSetup(){
    /*标题栏*/
    setWindowTitle("MicroWorks CAE res0.0.12.24-code0.0.01.07");
    //setWindowTitle("基于ABAQUS求解器的激光冲击仿真专用软件V1.0");
    setWindowIcon(QIcon("Resources/icons/logo.png"));
    setFont(QFont("Microsoft YaHei", 9));
    setAcceptDrops(true);

    mouse_pos->setStyleSheet("QPushButton{border:none;font: 9pt '微软雅黑';}");
    mouse_pos->setIcon(QIcon("resources/icons/icon_mouse_pos.png"));

    workspace_btn = new QPushButton(this);
    workspace_btn->setStyleSheet("QPushButton{border:none;font: 9pt '微软雅黑';}");
    workspace_btn->setIcon(QIcon("resources/icons/icon_worksapce_dir.png"));
    workspace_btn->setText(QString("工作目录: %1").arg(INSTANCE(Application)->getWorkSpace()));
    connect(workspace_btn, &QPushButton::clicked, this, &MainWindow::setWorkspace);

    /*状态栏*/
    status_bar->setFixedHeight(38);
    status_bar->addPermanentWidget(mouse_pos);
    status_bar->insertWidget(0, workspace_btn, 0);

    setStatusBar(status_bar);
    /*软件LOGO*/
    logo_btn->setIcon(QIcon("Resources/icons/icon_microworks.png"));
    logo_btn->setStyleSheet("QToolButton{border:none;}");
    logo_btn->setIconSize(QSize(35, 120));
    connect(logo_btn, &QToolButton::clicked, this, LAM() {
        QMessageBox::information(this, tr("版本号"), tr("开发版本dev-0.0.12.24"), QMessageBox::Yes | QMessageBox::Cancel | QMessageBox::Help);
        });
    /* 隐藏标题栏*/
    tab_dock->setTitleBarWidget(logo_btn);
    tab_dock->setFeatures(QDockWidget::DockWidgetVerticalTitleBar);
    tab_dock->setWidget(INSTANCE(ToolTabWidget));
    //INSTANCE(ToolTabWidget)->setParent(tab_dock);
    addDockWidget(Qt::TopDockWidgetArea, tab_dock);
    /*悬浮在渲染窗口上的便捷工具栏*/
    //float_bar; 不指定父对象能悬浮在父窗口上
    /*OCC建模窗口、VTK窗口*/
    setCentralWidget(INSTANCE(CenterWidget));
    INSTANCE(CenterWidget)->installEventFilter(this);
    VTK_RECEIVEER->setExchange(INSTANCE(DataExchange));
    /*菜单栏*/
    setMenuBar(INSTANCE(MenuBar));
    /*左边任务树*/
    addDockWidget(Qt::LeftDockWidgetArea, INSTANCE(TreeWidget));
    /*下方的线程管理器和日志窗口*/
    INSTANCE(BottomWidget)->addWidget(INSTANCE(LoggerWidget), QIcon("resources/icons/icon_logger_tab.png"), "日志");
    INSTANCE(BottomWidget)->addWidget(INSTANCE(AiWidget), QIcon("resources/icons/icon_ai_software.png"), "AI交互");
    addDockWidget(Qt::BottomDockWidgetArea, INSTANCE(BottomWidget));
    /*右边属性窗口，默认不显示*/
    addDockWidget(Qt::LeftDockWidgetArea, INSTANCE(ThreadWidget));
    /*全局材料属性的窗口*/
    addDockWidget(Qt::RightDockWidgetArea, current_dock);
    current_dock->setMinimumWidth(400);
    current_dock->hide();

}


void MainWindow::closeEvent(QCloseEvent* event){
    QMessageBox::StandardButton reply;
    reply = QMessageBox::question(this, 
        tr("退出程序"), 
        QString(tr("确认退出程序? 请保存数据后退出!")), 
        QMessageBox::Yes | QMessageBox::No);
    if (reply == QMessageBox::Yes){
        event->accept(); 
    }
    else{
        event->ignore();
    }
}



void MainWindow::dragEnterEvent(QDragEnterEvent* event) {
    if (event->mimeData()->hasUrls())                    // 数据中是否包含URL
        event->acceptProposedAction();                  // 如果是则接收动作
    else event->ignore();                               // 否则忽略该事件
}



void MainWindow::dropEvent(QDropEvent* event) {
    const QMimeData* mimeData = event->mimeData();      // 获取MIME数据
    if (mimeData->hasUrls()) {                            // 如果数据中包含URL
        QList<QUrl> urlList = mimeData->urls();         // 获取URL列表
        // 将其中第一个URL表示为本地文件路径
        QString filename = urlList.at(0).toLocalFile();
        if (!filename.isEmpty()) {                        // 如果文件路径不为空
            int current_center_widget_id = INSTANCE(CenterWidget)->getCurrentCenterWidgetId();
            if (current_center_widget_id != -1) {
                emit actionOpenModelSignal(filename, current_center_widget_id);
            }
        }
    }
}




void MainWindow::setRightDock(QWidget* dock) {
    if (dock != nullptr) {
        current_dock->setWidget(dock);
        current_dock->setWindowTitle(dock->windowTitle());
        current_dock->show();
        //connect(dock, &QWidget::dialogCancelSignal, right_dock, &QDockWidget::hide);
    }

}


void MainWindow::openModelEvent(){
    QString filename = QFileDialog::getOpenFileName(this, tr("打开几何模型文件"), "",
        tr("常见几何文件(*.step; *stp; *.Iges; *.igs; *.brep; *.stl);; Abaqus输入文件(*.inp)"));
    if (filename.isEmpty()) {
        INSTANCE(Logger)->log(QString("导入路径为空."), NSLogger::LOGERROR);
		return;
    }
    /*使用多线程进行读取几何模型文件*/
    else {
        int current_center_widget_id = INSTANCE(CenterWidget)->getCurrentCenterWidgetId();
        if (current_center_widget_id != -1) {
            emit actionOpenModelSignal(filename, current_center_widget_id);
        }
    }
}


void MainWindow::openRobotEvent(){
    QString filename = QFileDialog::getOpenFileName(
        this,
        tr("打开几何模型文件"), "",
        tr("常见几何文件(*.step; *stp;)"));
    if (filename.isEmpty()) {
        INSTANCE(Logger)->log(QString("导入路径为空."), NSLogger::LOGERROR);
		return;
    }
    /*使用多线程进行读取几何模型文件*/
    else {
        int current_center_widget_id = INSTANCE(CenterWidget)->getCurrentCenterWidgetId();
        if (current_center_widget_id != -1) {
            emit actionOpenRobotSignal(filename, current_center_widget_id);
        }
    }
}


void MainWindow::setWorkspace(){
    QString work = QFileDialog::getExistingDirectory(this,
        "请选择工作路径",
        QCoreApplication::applicationDirPath());
    if (!work.isEmpty()) {
        //QDir::setCurrent(work);
        INSTANCE(Application)->setWorkSpace(work);
        INSTANCE(Logger)->log(QString("工作路径已经修改为: %1. ").arg(work));
        INSTANCE(PluginManager)->loadPlugins();
        workspace_btn->setText(QString("工作目录: %1").arg(work));
    }

}



void MainWindow::subWindowResizeEvent(bool ismax){
    /*如果vtkWidget不是最大化，那么隐藏悬浮工具栏*/
    for (auto& actor : VTK_RECEIVEER->getInfoActors2D()) {
        actor->SetVisibility(ismax);
    }
    if (ismax) {
        INSTANCE(FloatBar)->show();
    }
    else {
        INSTANCE(FloatBar)->hide();
    }
    VTK_RECEIVEER->renderUpdateEvent();
}



void MainWindow::setCurrentRenderWidget(NSVtkWidget::VtkWidget* vtk){
    /*重新调用setExchange, 不然VTK_RECEIVEER中的exchange为nullptr*/
	vtk->setExchange(INSTANCE(DataExchange));

}






void MainWindow::globalPosEvent(QPoint pos) {
    mouse_pos->setText(QString("全局坐标 X:%1, Y:%2.").arg(pos.x()).arg(pos.y()));

}




NAMESPACE_END
